package com.robot.base.util

import android.graphics.Bitmap
import android.os.Environment
import android.util.Log
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.robot.base.view.MaterialDesignDialog
import me.goldze.mvvmhabit.base.BaseActivity
import java.io.*
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL

object Util {
    private const val TAG = "SDK_Sample.Util"
    fun bmpToByteArray(bmp: Bitmap, needRecycle: Boolean): ByteArray {
        val output = ByteArrayOutputStream()
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output)
        if (needRecycle) {
            bmp.recycle()
        }
        val result = output.toByteArray()
        try {
            output.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return result
    }

    fun getHtmlByteArray(url: String?): ByteArray? {
        var htmlUrl: URL? = null
        var inStream: InputStream? = null
        try {
            htmlUrl = URL(url)
            val connection = htmlUrl.openConnection()
            val httpConnection = connection as HttpURLConnection
            val responseCode = httpConnection.responseCode
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.inputStream
            }
        } catch (e: MalformedURLException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return inputStreamToByte(inStream)
    }

    fun inputStreamToByte(`is`: InputStream?): ByteArray? {
        try {
            val bytestream = ByteArrayOutputStream()
            var ch: Int
            while (`is`!!.read().also { ch = it } != -1) {
                bytestream.write(ch)
            }
            val imgdata = bytestream.toByteArray()
            bytestream.close()
            return imgdata
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun readFromFile(fileName: String?, offset: Int, len: Int): ByteArray? {
        var len = len
        if (fileName == null) {
            return null
        }
        val file = File(fileName)
        if (!file.exists()) {
            Log.i(TAG, "readFromFile: file not found")
            return null
        }
        if (len == -1) {
            len = file.length().toInt()
        }
        Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len))
        if (offset < 0) {
            Log.e(TAG, "readFromFile invalid offset:$offset")
            return null
        }
        if (len <= 0) {
            Log.e(TAG, "readFromFile invalid len:$len")
            return null
        }
        if (offset + len > file.length().toInt()) {
            Log.e(TAG, "readFromFile invalid file len:" + file.length())
            return null
        }
        var b: ByteArray? = null
        try {
            val `in` = RandomAccessFile(fileName, "r")
            b = ByteArray(len)
            `in`.seek(offset.toLong())
            `in`.readFully(b)
            `in`.close()
        } catch (e: Exception) {
            Log.e(TAG, "readFromFile : errMsg = " + e.message)
            e.printStackTrace()
        }
        return b
    }

}